<template>
  <div class="page-register">
    <article class="header">
      <header>
        <a class="site-logo" href="http://www.meituan.com">美团</a>
        <span class="login">
          <em class="bold">已有美团账号？</em>
          <a href="/login">
            <el-button
              type="primary"
              size="small"
            >登录</el-button>
          </a>
        </span>
      </header>
    </article>
    <section>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="昵称"
          prop="name"
        >
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item
          label="邮箱"
          prop="email"
        >
          <el-input v-model="ruleForm.email" />
          <el-button
            size="mini"
            @click="sendMsg"
          >
            免费获取邮箱验证码
          </el-button>
          <span class="status">{{ statusMsg }}</span>
        </el-form-item>
        <el-form-item
          label="验证码"
          prop="code"
        >
          <el-input
            v-model="ruleForm.code"
            maxlength="4"
          />
        </el-form-item>
        <el-form-item
          label="创建密码"
          prop="pwd"
        >
          <el-input
            v-model="ruleForm.pwd"
            type="password"
          />
        </el-form-item>
        <el-form-item
          label="确认密码"
          prop="cpwd"
        >
          <el-input
            v-model="ruleForm.cpwd"
            type="password"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="register"
          >
            同意以下协议并注册
          </el-button>
          <div class="error">
            {{ error }}
          </div>
        </el-form-item>
        <el-form-item>
          <a class="f1" href="http://www.meituan.com/about/terms" target="_blank">《美团网用户协议》</a>
          <a class="f1" href="https://rules-center.meituan.com/rules-detail/2" target="_blank">《美团点评隐私政策》</a>
        </el-form-item>
      </el-form>
    </section>
    <footer class="footer--mini">
      <p class="copyright">
        ©<a class="f1" href="https://www.meituan.com">meituan.com</a>&nbsp;
        <a class="f1" target="_blank" href="http://www.miibeian.gov.cn/">京ICP证070791号</a>&nbsp;
        <span class="f1">京公网安备11010502025545号</span>
      </p>
    </footer>
  </div>
</template>

<script>
import CryptoJS from 'crypto-js'
export default {
  data () {
    const validator = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pwd) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      statusMsg: '',
      error: '',
      ruleForm: {
        name: '',
        code: '',
        pwd: '',
        cpwd: '',
        email: ''
      },
      rules: {
        name: [{ required: true, type: 'string', message: '请输入昵称', trigger: 'blur' }],
        email: [{ required: true, type: 'email', message: '请输入邮箱', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        pwd: [{ required: true, message: '请输入验证密码', trigger: 'blur' }],
        cpwd: [{ required: true, message: '确认密码', trigger: 'blur' }, { validator, trigger: 'blur' }]
      }
    }
  },
  layout: 'blank',
  methods: {
    async sendMsg () {
      let namePass
      let emailPass
      if (this.timerid) {
        return false
      }
      this.$refs.ruleForm.validateField('name', (valid) => {
        namePass = valid
      })
      this.statusMsg = ''
      if (namePass) {
        return false
      }
      this.$refs.ruleForm.validateField('email', (valid) => {
        emailPass = valid
      })
      if (!namePass && !emailPass) {
        await this.$axios.post('/users/verify', {
          username: this.ruleForm.name,
          email: this.ruleForm.email
        })
        let count = 60
        this.statusMsg = `验证码已发送,剩余${count--}秒`
        this.timerid = setInterval(() => {
          this.statusMsg = `验证码已发送,剩余${count--}秒`
          if (count === 0) {
            clearInterval(this.timerid)
            this.statusMsg = ''
          }
        }, 1000)
      }
    },
    register () {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.$axios.post('/users/signup', {
            username: this.ruleForm.name,
            password: CryptoJS.MD5(this.ruleForm.pwd).toString(),
            email: this.ruleForm.email,
            code: this.ruleForm.code
          })
          await this.$message.success('注册成功')
          location.href = '/'
        }
      })
    }
  }
}
</script>

<style lang="scss">
  @import "assets/css/register/index.scss";
</style>
